package com.project.service.impl;

import com.project.dto.ReportIdNameDto;
import com.project.dto.ReportReviewListDTO;
import com.project.mapper.ProjectReportMapper;
import com.project.mapper.ReportContentMapper;
import com.project.model.entity.ProjectReport;
import com.project.model.entity.ReportContent;
import com.project.service.ExportService;
import com.project.service.ProjectReportService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class ProjectReportServiceImpl implements ProjectReportService {
    private final ProjectReportMapper projectReportMapper;
    private final ReportContentMapper reportContentMapper;
    @Autowired
    private ExportService exportService;

    @Autowired
    public ProjectReportServiceImpl(ProjectReportMapper projectReportMapper, ReportContentMapper reportContentMapper) {
        this.projectReportMapper = projectReportMapper;
        this.reportContentMapper = reportContentMapper;
    }

    @Override
    public ProjectReport addProjectReport(ProjectReport projectReport) {
        projectReportMapper.insertProjectReport(projectReport);
        return projectReport;
    }

    @Override
    public void deleteProjectReport(Integer id) {
        projectReportMapper.deleteProjectReportById(id);
    }

    @Override
    public ProjectReport updateProjectReport(ProjectReport projectReport) {
        projectReportMapper.updateProjectReport(projectReport);
        return projectReport;
    }

    @Override
    public ProjectReport getProjectReportById(Integer id) {
        return projectReportMapper.selectProjectReportById(id);
    }

    @Override
    public ProjectReport getProjectReportByReportId(Integer id) {
        return projectReportMapper.selectProjectReportByReportId(id);
    }

    @Override
    public List<ProjectReport> getAllProjectReports() {
        // 需求
        // 设计
        // 构建
        // 测试
        // 实施
        String[] contents = new String[]{"需求","设计","构建","测试","实施"};

        List<ProjectReport> projectReports = projectReportMapper.selectAllProjectReports();
        List<List<ReportContent>> manyContentLists = new ArrayList<>();
        for (ProjectReport projectReport : projectReports) {
            Integer projectId = projectReport.getId();
            List<ReportContent> reportContents = reportContentMapper.selectReportContentsByReportId(projectId);
            if (!reportContents.isEmpty()) {
                continue;
            }
            int total = 0;
            int percent = 0;
//            List<ReportContent> contentList = new ArrayList<>();
            for (String content : contents) {
                Random r = new Random();
                percent = r.nextInt(100-total)+1;
                total += percent;
                ReportContent reportContent = new ReportContent();
                reportContent.setStage(content);
                reportContent.setReportId(projectId);
                reportContent.setPercentage(percent);
//                contentList.add(reportContent);
                reportContentMapper.insertReportContent(reportContent);
            }
        }
        return projectReports;
    }

    @Override
    public String updateProjectReportStatus(Integer reportId, String status) {
        ProjectReport report = projectReportMapper.selectProjectReportById(reportId);
        report.setStatus(status);
        updateProjectReport(report);
        return status;
    }


    @Override
    public List<ReportIdNameDto> getReportIdName() {
        return projectReportMapper.getReportIdName();
    }

    @Override
    public List<ReportReviewListDTO> getReportReviewList(String status) {
        return projectReportMapper.getReportReviewList(status);
    }

    public void checkDraft(String reportId){
        try {
            Integer id=Integer.valueOf(reportId);
            updateProjectReportStatus(id,"未通过");
            // 获取 Word 文档
            XWPFDocument document = exportService.exportReportDateWord(reportId);
            exportService.upload(document,reportId);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}

